#include "Character.h"

Character::Character(btDiscreteDynamicsWorld* pWorld,
                     btCylinderShape* pCollisionShape,
                     Ogre::SceneManager* pSceneManager,
                     OpenSteer::Vec3 position,
                     const char* mesh):
  _pWorld(pWorld),
  _pCollisionShape(pCollisionShape),
  _pSceneManager(pSceneManager)
{
  // Ogre --------------------------------------------------------------

  // Entidad
  _pEntity = _pSceneManager->createEntity(mesh);

  // Nodo
  _pNode = _pSceneManager->getRootSceneNode()->createChildSceneNode();
  _pNode->attachObject(_pEntity);

  // Bullet ------------------------------------------------------------

  // MotionState
  btVector3 bulletPosition = openSteerToBullet(position);

  // Situar la base del personaje sobre el plano XZ
  bulletPosition.setY(_pCollisionShape->getHalfExtentsWithoutMargin().y());

  _pMotionState =
    new MyMotionState(btTransform(btQuaternion(0, 0, 0, 1),
                                  bulletPosition), _pNode);

  // ConstructionInfo
  btScalar mass = 1;
  btVector3 fallInertia(0,0,0);
  _pCollisionShape->calculateLocalInertia(mass, fallInertia);

  btRigidBody:: btRigidBodyConstructionInfo
    bodyCI(mass,_pMotionState, _pCollisionShape,
                 fallInertia);

  // Cuerpo rígido
  _pRigidBody = new btRigidBody(bodyCI);
  _pRigidBody->setLinearFactor(btVector3(1,0,1));
  _pRigidBody->setAngularFactor(btVector3(0,0,0));

  _pWorld->addRigidBody(_pRigidBody);

  // OpenSteer ---------------------------------------------------------

  // Bullet vehicle
  setRigidBody(_pRigidBody);
  setRadius(_pCollisionShape->getRadius());
  setMaxForce(0);
  setMaxSpeed(0);
}

inline int Character::getLife() {
  return _life; }

inline void Character::setLife(int life) {
  _life = life; }
